<?php
namespace Partner\Controller;
use Utuo\BackgroundController;
class PartnerRolesController extends BackgroundController
{
   private $status=array(
		'1'=>'开启',
		'0'=>'关闭'	
	);

	private $tableNames; // 所有权限表的模型名 
	public function _initialize() {
		$this->setControllerTitle('合作商角色管理');
		$this->setActionTitle(array(
				'index' => '合作商角色列表',
				'add' => '添加合作商角色',
				'modify' => '修改合作商角色',
				'recycle' => '回收站',
				'access'=>'配置权限'
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
		$rbacPartner = C('RBAC_PARTNER');
		$this->tableNames = \Utuo\Rbac::getAuthModelNames( array('role' => $rbacPartner['RBAC_ROLE_TABLE'], 'user' => $rbacPartner['RBAC_USER_TABLE'], 'access' => $rbacPartner['RBAC_ACCESS_TABLE'], 'node' => $rbacPartner['RBAC_NODE_TABLE']) );
	}
	public function getLists($is_deleted) {
		$condition = array(
				'pid' => $this->aid,
				'is_deleted' => $is_deleted,
		);
		$role = M($this->tableNames['role'][1])->where($condition)->select();
		
		$this->assign("role",$role);
		$this->display();
	}
	
	public function addAction() {
		$role['radio'] = \Utuo\Form::getRadio("status", $this->status, 1);
		$this->assign('role',$role);
		$this->display('save');
	}
	
	public function modifyAction($id) {
		$role = M($this->tableNames['role'][1])->getById($id);
		$role['radio']=\Utuo\Form::getRadio("status", $this->status, $role['status']);
		$this->assign('role', $role);
		$this->display('save');
	}
	
	public function accessAction( $rid ) {
		/*获得角色已有的权限*/
		$nodeIds = M($this->tableNames['access'][1])->where('role_id='.$rid)->getField('node_id',true);
		$nodeIds = implode($nodeIds,',');
		/*已分配给合作商可分配客户的节点*/
		$m = M('ParnterNode p');
		$nodes = $m->join('__NODE__ n on p.node_id=n.id')->where('n.status=1 and n.is_deleted=0 and p.partner_id='.$this->aid)->select();
		
		// 获得客户的模块
		$rbacModule=C('RBAC_MODULE');
		$node = M('node')->where("name='".$rbacModule['MODULE_PARTNER']."' and pid=0 and level=1")->find();
	    $nodes = makeListLevel($nodes, $node['id'], 'pid');
	  
		$node['child'] = $nodes;
		$this->assign("rid",$rid);
		$this->assign("nodeIds",$nodeIds);
		$this->assign('nodes',$node);
		$this->display();
	}
	
	public function setAccessAction() {
		$role_id = I('role_id');
		M($this->tableNames['access'][1])->where('role_id='.$role_id)->delete();
		$array = I('access');
		$temparray = array();
		foreach ($array as $v) {
			$temp = explode('-', $v);
			$temparray[]=array(
					'role_id'=>$role_id,
					'node_id'=>$temp[0],
					'level'=>$temp[1],
			);
		}
		!$temparray and $this->ajaxSuccess('配置成功');
		M($this->tableNames['access'][1])->addAll($temparray)!==false?$this->ajaxSuccess('配置成功'):$this->ajaxError('配置失败');
	}
	
	// 保存
	public function saveAction() {
		$this->submitPost($this->tableNames['role'][1]);
	}
	
	// 提交角色
	public function submitPost( $model ) {
		$model = is_object($model) ? $model : D($model);
		if ($model->isCreated or $model->create()) {
			$model->pid= $this->aid; // 合作商管理客户的公共角色cid为0
			if (empty($_REQUEST['id'])) {
				unset( $model->id );
				if ($model->add()) {
					$this->ajaxSuccess("添加成功！");
				} else {
					$this->ajaxError("添加失败！".$model->getError());
				}
			} else {
				if (false !== $model->save()) {
					$this->ajaxSuccess("修改成功！");
				} else {
					$this->ajaxError("修改失败！");
				}
			}
		} else {
			$this->ajaxError($model->getError());
		}
	}
	
}